﻿using SWENAssignment.Sophia.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SWENAssignment.Sophia
{
    public partial class addCheckInDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblCustomerID.Text = Request.QueryString["customerID"];
            lblReservationID.Text = Request.QueryString["reservationID"];
            lblNumOfAdults.Text = Request.QueryString["numOfAdults"];
            lblNumOfChildren.Text = Request.QueryString["numOfChildren"];
            lblCheckInDate.Text = DateTime.Now.ToString();
            lblRoomNum.Text = Request.QueryString["roomNum"];
            lblpreferredRoomType.Text = Request.QueryString["preferredRoomType"];
        }

        protected void btnCheckIn_Click(object sender, EventArgs e)
        {
            string c_checkInID = tbxCheckInID.Text;
            bool c = DBmanager.checkInIDIsExist(c_checkInID);
            if (c == true)
            {
                lblStatus.Text = "Check-In ID already exist. Please re-enter another Check-In ID!";
            }
            else
            {
                string r_checkInID = tbxCheckInID.Text;
                string r_customerID = lblCustomerID.Text;
                string r_reservationID = lblReservationID.Text;
                int r_numOfAdults = Convert.ToInt32(lblNumOfAdults.Text);
                int r_numOfChildren = Convert.ToInt32(lblNumOfChildren.Text);
                DateTime r_checkInDate = Convert.ToDateTime(lblCheckInDate.Text);
                DateTime r_checkOutDate = Convert.ToDateTime(ddlDate2.SelectedValue + ddlMonth2.SelectedValue + tbxYear2.Text);
                string r_roomNum = lblRoomNum.Text;
                string r_paymentType = ddlPaymentType.SelectedValue;
                string r_checkInStatus = lblCheckInStatus.Text;

                checkInDetails newCheckIn = new checkInDetails(r_checkInID, r_customerID, r_reservationID, r_numOfAdults, r_numOfChildren, r_checkInDate, r_checkOutDate, r_roomNum, r_paymentType, r_checkInStatus);

                if (DBmanager.insertCheckInDetails(newCheckIn) == 1)
                {
                    string checkOut = ddlDate2.SelectedValue + "/" + ddlMonth2.SelectedValue + "/" + tbxYear2.Text;
                    DBmanager.UpdateCheckInStatus(lblReservationID.Text, "Checked-In");
                    Session["checkInID"] = r_checkInID;
                    string querystring = "checkInID=" + tbxCheckInID.Text + "&customerID=" + lblCustomerID.Text + "&reservationID=" + lblReservationID.Text + "&numOfAdults=" + lblNumOfAdults.Text + "&numOfChildren=" + lblNumOfChildren.Text + "&CheckInDate=" + lblCheckInDate.Text + "&CheckOutDate=" + checkOut + "&roomNum=" + lblRoomNum.Text + "&paymentType=" + ddlPaymentType.SelectedValue + "&checkInStatus=" + lblCheckInStatus.Text;
                    Server.Transfer("~/Sophia/addCustomersDetailsSuccessfully.aspx?" + querystring);
                }
                else
                {
                    lblStatus2.Text = "Check-In failed!";
                }



            }
        }
    }
}